package mySpringboot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by jiangfeng on 2018/2/25.
 */
//表明这是一个 Controller
@Controller
//RestController是一种Rest风格的Controller，可以直接返回对象而不返回视图，返回的对象可以使JSON，XML等
//@RestController
//使用自动配置，主动添加并解析bean，配置文件等信息
//@EnableAutoConfiguration
@SpringBootApplication
public class HelloSpringBoot  extends SpringBootServletInitializer {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    // 改成war包形式 要覆盖此方法
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(HelloSpringBoot.class);
    }

    // 访问的url
    @RequestMapping("/hello")
    //表示返回JSON格式的结果，如果前面使用的是@RestController可以不用写
    @ResponseBody
    String hello(){
        logger.info("hehe");
        return "hello springboot!";
    }

    @RequestMapping("/log")
    @ResponseBody
    public Object writeLog()
    {
        logger.debug("This is a debug message");
        logger.info("This is an info message");
        logger.warn("This is a warn message");
        logger.error("This is an error message");
        return "OK";
    }

    private int count =0;
    @RequestMapping("/calc")
    public String calc(){
        logger.info("calc num:"+(++count));
        return "calc";
    }

    public static void main(String[] args) {
        //通过SpringApplication的run()方法启动应用，无需额外的配置其他的文件
        SpringApplication.run(HelloSpringBoot.class, args);
    }

}
